package com.etalk.crm.controller;

import com.alibaba.fastjson.JSONObject;
import com.etalk.crm.pojo.NewMainBean;
import com.etalk.crm.pojo.User;
import com.etalk.crm.pojo.UserPanelSort;
import com.etalk.crm.service.CCNewMainService;
import com.etalk.crm.service.YKNewMainService;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.etalk.crm.utils.PubResult.pubResultFunction;

/**
 * @author James
 * @Date: 2018/12/18 14:01
 * @Description:
 */
@Controller
@RequestMapping(value = "/YkNewMainCtl",produces = "application/json; charset=utf-8")
public class YKNewMainManageCtl {
    protected static final Logger logger = LogManager.getLogger(YKNewMainManageCtl.class);

    @Resource
    private YKNewMainService ykNewMainService;

    @Value("${web.crm.old.url}")
    String oldCrmUrl;
    
    /**
     * @Author James
     * @Description 各个角色 的 首页
     * @Date 14:08 2018/12/18
     * @Param
     * @return 
     **/
    @RequestMapping(value = "/newMain",method = RequestMethod.GET)
    public String newMain(HttpSession session, Model model){
        int roleId = Integer.parseInt(session.getAttribute("roleId").toString());
        int storesId=Integer.parseInt(session.getAttribute("storesId").toString());
        model.addAttribute("roleId",roleId);
        logger.info(session.getAttribute("storesId").toString());
        List<User> ccOrSSCList = null;
        if(roleId == 12){
            ccOrSSCList = ykNewMainService.searchCcByStoresId(storesId);
        }else if(roleId == 9){
            ccOrSSCList = ykNewMainService.searchSscByStoresId(storesId);
        }
        model.addAttribute("ccOrSSCList",ccOrSSCList);
        model.addAttribute("loginName",session.getAttribute("loginname"));
        model.addAttribute("oldCrmUrl",oldCrmUrl);
        return "newMainManage/newMain";
    }

    /**
     *首页cc 预约与回访(提醒)
     * @param page
     * @param limit
     * @param searchDate
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectTrackRemindByBccId",method = RequestMethod.POST)
    @ResponseBody
    public String selectTrackRemindByBccId(Integer bccId,Integer page, Integer limit, String searchDate, HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        if(bccId == null){
            bccId = Integer.parseInt(session.getAttribute("userid").toString());
        }
        PageInfo<NewMainBean> pageInfo = ykNewMainService.selectTrackRemindByBccId(bccId,searchDate,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页约课顾问提醒列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }


    /**
     *查询首页约课顾问下  潜在学员
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectMainTipPotentialByBccId",method = RequestMethod.POST)
    @ResponseBody
    public String selectMainTipPotentialByBccId(Integer page, Integer limit,String searchDate,
            Integer state ,HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        int bccId = Integer.parseInt(session.getAttribute("userid").toString());
        PageInfo<NewMainBean> pageInfo = ykNewMainService.selectMainTipPotentialByBccId(bccId,state,page,limit,searchDate);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("首页约课顾问下潜在学员和待约学员加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *约克顾问下未完成demo 课学员
     * @param page
     * @param limit
     * @param searchDate
     * @param session
     * @return
     */
    @RequestMapping(value = "/unfinishDemoCourseByBccId",method = RequestMethod.POST)
    @ResponseBody
    public String unfinishDemoCourseByBccId(Integer page, Integer limit, String searchDate,HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        int bccId = Integer.parseInt(session.getAttribute("userid").toString());
        PageInfo<NewMainBean> pageInfo = ykNewMainService.unfinishDemoCourseByBccId(bccId,searchDate,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("约克顾问下未完成demo课学员加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *约克顾问下demo课已完成学员
     * @param page
     * @param limit
     * @param searchDate
     * @param session
     * @return
     */
    @RequestMapping(value = "/demoFinishedListByBccId",method = RequestMethod.POST)
    @ResponseBody
    public String demoFinishedListByBccId(Integer page, Integer limit, String searchDate,HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        int bccId = Integer.parseInt(session.getAttribute("userid").toString());
        PageInfo<NewMainBean> pageInfo = ykNewMainService.demoFinishedListByBccId(bccId,searchDate,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("约克顾问下demo课已完成学员加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *约克顾问下已约未上学员
     * @param page
     * @param limit
     * @param searchDate
     * @param session
     * @return
     */
    @RequestMapping(value = "/demoMissedListByBccId",method = RequestMethod.POST)
    @ResponseBody
    public String demoMissedListByBccId(Integer page, Integer limit, String searchDate,HttpSession session){
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }
        int bccId = Integer.parseInt(session.getAttribute("userid").toString());
        PageInfo<NewMainBean> pageInfo = ykNewMainService.demoMissedListByBccId(bccId,searchDate,page,limit);
        Map<String,Object> jsonMap = pubResultFunction(pageInfo);
		logger.info("约克顾问下demo课已约未上学员加载成功");
        return JSONObject.toJSONString(jsonMap);
    }

    /**
     *查询用户面板  排序规则面板
     * @param page
     * @param limit
     * @param searchDate
     * @param session
     * @return
     */
    @RequestMapping(value = "/selectSortByUser",method = RequestMethod.POST)
    @ResponseBody
    public String selectSortByUser(Integer page, Integer limit, String searchDate,HttpSession session){
        List<UserPanelSort > userPanelSortList = ykNewMainService.selectSortByUser(Integer.parseInt(session.getAttribute("userid").toString()));
        Map<String,Object> jsonMap = new HashMap<>(2);
        jsonMap.put("userPanelSortList",userPanelSortList);
        jsonMap.put("code",1);
		logger.info("用户面板排序规则列表加载成功");
        return JSONObject.toJSONString(jsonMap);
    }
    
    
    /*
     * @Author James
     * @Description 添加用户排序规则
     * @Date 18:53 2019/1/4
     * @Param 
     * @return 
     **/
    @RequestMapping(value = "/addUserSort",method = RequestMethod.POST)
    @ResponseBody
    public String addUserSort(HttpSession session,@RequestBody List<UserPanelSort> config){
        logger.info("用户排序规则");
        int result = ykNewMainService.addUserSort(config,Integer.parseInt(session.getAttribute("userid").toString()));
        Map<String,Object> jsonMap = new HashMap<>();
        if(result > 0){
            //规则排序提交成功
            jsonMap.put("code",1);
            logger.info("用户排序规则成功");
        }else{
             //规则排序提交失败
            jsonMap.put("code",-1);
            logger.info("用户排序规则失败");
        }
        return JSONObject.toJSONString(jsonMap);
    }

    /*
     * @Author James
     * @Description 查询学员属性列表
     * @Date 13:52 2019/1/8
     * @Param
     * @return
     **/
    @RequestMapping(value = "/revisitInfoList",method = RequestMethod.POST)
    @ResponseBody
    public String selectRevisitInfo(){
        logger.info(" 查询学员属性列表");
        List<Map<String, Object>> revisitInfoList = ykNewMainService.selectRevisitInfo();
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("code",1);
        jsonMap.put("revisitInfoList",revisitInfoList);
        return JSONObject.toJSONString(jsonMap);
    }
}
